import sqlite3
import os

# 连接到数据库
conn = sqlite3.connect('./openmemory.db')
cursor = conn.cursor()

# 查询所有名为openmemory的应用
cursor.execute("SELECT id, name, created_at FROM apps WHERE name = 'openmemory' ORDER BY created_at")
apps = cursor.fetchall()

print('所有名为openmemory的应用:')
for app in apps:
    print(f'ID: {app[0]}, Name: {app[1]}, Created: {app[2]}')

# 查询每个应用的记忆数量
print('\n每个应用的记忆数量:')
for app in apps:
    cursor.execute("SELECT COUNT(*) FROM memories WHERE app_id = ?", (app[0],))
    count = cursor.fetchone()[0]
    print(f'App ID {app[0]}: {count} 条记忆')

# 查询最新的5条记忆
cursor.execute("SELECT id, content, app_id, created_at FROM memories ORDER BY created_at DESC LIMIT 5")
memories = cursor.fetchall()

print('\n最新的5条记忆:')
for memory in memories:
    print(f'ID: {memory[0]}, App ID: {memory[2]}, Created: {memory[3]}')
    print(f'Content: {memory[1][:100]}...')
    print()

conn.close()